// TOWN SCRIPT
//    Town 12: Sweon

// This is the special encounter script for this town.
// The states INIT_STATE, EXIT_STATE, and START_STATE have
// meanings that are described in the documenation. States you write
// yourself should be numbered from 10-100.

begintownscript;

variables;

short i,j,k,r1,x,y,choice;

body;

beginstate INIT_STATE;
	set_name(6,"Wonred");		
	set_name(7,"Hildeburh");	
	set_name(8,"Hraedla");
	set_name(9,"Garmund");		
	set_name(10,"Ongen");
	set_name(11,"Ongen");					
	set_name(18,"Srendrac");	
	set_state_continue(10);
break;

beginstate EXIT_STATE;
break;

beginstate START_STATE;
	x = 0;
while (x < 48) {
	y = 0;
	while (y < 48) {
	if (is_field_on_space(x,y,6)) {
	flip_terrain(x - 1,y);
	flip_terrain(x + 1,y);		
	flip_terrain(x,y - 1);
	flip_terrain(x,y + 1);			
	move_item_on_spot(x,y,-1,-1);		
	if (get_terrain(x,y) != 0)
	set_terrain(x,y,0);	
	if (get_floor(x,y) != 89)	
	set_floor(x,y,89);
	}	
		y = y + 1;
	}
	x = x + 1;
	}
	
break;

beginstate 10;
	if (get_flag(100,0) >= 14)
		set_state_continue(11);
break;

beginstate 11;
	if (get_flag(12,0) == 250)
		end();
	reset_dialog();
	add_dialog_str(0,"Boom.",0);
	add_dialog_str(1,"Before you can react, massive fireballs explode in the various buildings of the town. The rough-built wooden and stone houses collapse within the blink of an eye. The fires race along the high grass, devouring everything in their paths.",0);
	add_dialog_choice(0,"OK.");
	choice = run_dialog(1);
	set_flag(12,0,250);
	set_state_continue(12);
break;

beginstate 12;
			put_field_on_space(38,27,6);
			put_field_on_space(24,24,6);
			put_field_on_space(9,9,6);
			put_field_on_space(12,33,6);
			activate_hidden_group(1);
			erase_char(10);			
			set_name(11,"Ongen");					
break;

beginstate 18;
	if (get_flag(100,0) == 14) {
	set_flag(100,0,15);
	set_flag(112,7,19);
	set_flag(12,9,14);
	}
break;

beginstate 23;
	if (has_special_item(3) > 0)
		set_state_continue(24);
	set_state_continue(25);
break;

beginstate 24;
	message_dialog("You show him the log. It gets his attention, at least briefly. _Ah, interesting piece of work. But no use if nobody doesn't know what's on there. Come back when you figure it out._","");
break;

beginstate 25;
	if (get_flag(100,0) >= 12)
	message_dialog("He furrows his brow. _Outpost? Never seen it. I've none to spare - go and take a look. Report back here if you get something._","");
break;

